﻿using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Globalization;
public partial class Main_Inventory_ViewPurchaseOrder : System.Web.UI.Page
{
    Datautility DU = new Datautility();
    string strsql = "";
    DataTable DTMain = new DataTable();
    DataTable DTPOMain = new DataTable();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["userid"] == null)
        {
            if (Request.QueryString["redirect"] != null)
            {
                if (Request.QueryString["redirect"].ToString() == "PM")
                {
                    Response.Redirect("~/Login.aspx?redirect=PM");
                }
            }
            else
            {
                Response.Redirect("~/SessionExpired.aspx");
            }
        }
        if (!IsPostBack)
        {
            BindCustomData();
            BindInternationalPO();
        }
    }
    protected void BindCustomData()
    {
        strsql = "Select POID,PoPrefix +'/'+ PoVoucherNo +'/'+ PoSuffix as PONo,convert(int,POVOUCHERNO) as POVOUCHERNO,PoDate,ACC_LEDMAST.LNAME,DeliveryDate,Priority,GrandTotal,Status,TotalQty as Quantity from ACC_POMAIN inner join ACC_LEDMAST on ACC_LEDMAST.LID= ACC_POMAIN.LID where BILLFORMATID=5 Order By convert(int,POVOUCHERNO) Desc";
        DataTable DT = DU.getdata(strsql);
        if (DT.Rows.Count > 0)
        {
            DataView dv = DT.DefaultView;
            if (this.ViewState["SortExpression"] != null)
            {
                dv.Sort = string.Format("{0} {1}", ViewState["SortExpression"].ToString(), this.ViewState["SortOrder"].ToString());
            }  
            grdDisplay.DataSource = DT;
            grdDisplay.DataBind();              
        }        
    }
    protected void BindInternationalPO()
    {
        strsql = "Select POID,PoPrefix +'/'+ PoVoucherNo +'/'+ PoSuffix as PONo,convert(int,POVOUCHERNO) as POVOUCHERNO,PoDate,ACC_LEDMAST.LNAME,DeliveryDate,Priority,GrandTotal,Status,TotalQty as Quantity from ACC_POMAIN inner join ACC_LEDMAST on ACC_LEDMAST.LID= ACC_POMAIN.LID where BILLFORMATID=4 Order By convert(int,POVOUCHERNO) Desc";
        DataTable DT = DU.getdata(strsql);
        if (DT.Rows.Count > 0)
        {
            DataView dv = DT.DefaultView;
            if (this.ViewState["SortExpression"] != null)
            {
                dv.Sort = string.Format("{0} {1}", ViewState["SortExpression"].ToString(), this.ViewState["SortOrder"].ToString());
            }
            grdInternational.DataSource = DT;
            grdInternational.DataBind();
        }
        #region
        //DTPOMain.Columns.Add("POID");
        //DTPOMain.Columns.Add("PoVoucherNo");
        //DTPOMain.Columns.Add("PoDate", typeof(DateTime));
        //DTPOMain.Columns.Add("Lname");
        //DTPOMain.Columns.Add("DeliveryDate");
        //DTPOMain.Columns.Add("Priority");
        //DTPOMain.Columns.Add("Quantity");
        //DTPOMain.Columns.Add("GrandTotal");
        //DTPOMain.Columns.Add("Status");
        //strsql = "Select * from ACC_POMAIN where BILLFORMATID=4";
        //DataTable DT = DU.getdata(strsql);
        //if (DT.Rows.Count > 0)
        //{
        //    for (int i = 0; i < DT.Rows.Count; i++)
        //    {
        //        string PoVoucherNo = "";
        //        string Prefix = "";
        //        string PoNumber = "";
        //        string Suffix = "";
        //        DataRow DRMain = DTPOMain.NewRow();
        //        DRMain[0] = DT.Rows[i]["POID"].ToString();

        //        if (DT.Rows[i]["PoPrefix"].ToString() != string.Empty)
        //        {
        //            Prefix = DT.Rows[i]["PoPrefix"].ToString() + "/";
        //        }
        //        if (DT.Rows[i]["PoVoucherNo"].ToString() != string.Empty)
        //        {
        //            PoNumber = DT.Rows[i]["PoVoucherNo"].ToString();
        //        }
        //        if (DT.Rows[i]["PoSuffix"].ToString() != string.Empty)
        //        {
        //            Suffix = "/" + DT.Rows[i]["PoSuffix"].ToString();
        //        }
        //        PoVoucherNo = Prefix + PoNumber + Suffix;
        //        DRMain[1] = PoVoucherNo;

        //        DRMain[2] = DT.Rows[i]["PoDate"].ToString();

        //        strsql = "Select LNAME from ACC_LEDMAST where LID='" + DT.Rows[i]["LID"].ToString() + "'";
        //        DataTable DTLed = DU.getdata(strsql);
        //        if (DTLed.Rows.Count > 0)
        //        {
        //            DRMain[3] = DTLed.Rows[0]["LNAME"].ToString();
        //        }
        //        if (DT.Rows[i]["DeliveryDate"].ToString() == "")
        //        {
        //            DRMain[4] = "--";
        //        }
        //        else
        //        {
        //            DRMain[4] = Convert.ToDateTime(DT.Rows[i]["DeliveryDate"].ToString()).ToString("dd-MMM-yyyy");
        //        }
        //        DRMain[5] = DT.Rows[i]["Priority"].ToString();
        //        DRMain[6] = Convert.ToDouble(DT.Rows[i]["TotalQty"].ToString()).ToString("N0");
        //        if (DT.Rows[i]["GrandTotal"].ToString() != string.Empty)
        //        {
        //            DRMain[7] = Convert.ToDouble(DT.Rows[i]["GrandTotal"].ToString()).ToString("N2");
        //        }
        //        else
        //        {
        //            DRMain[7] = "0.00";
        //        }
        //        DRMain[8] = DT.Rows[i]["Status"].ToString();
        //        DTPOMain.Rows.Add(DRMain);
        //    }
        //}
        //DataView dv = DT.DefaultView;
        //if (this.ViewState["SortExpression"] != null)
        //{
        //    dv.Sort = string.Format("{0} {1}", ViewState["SortExpression"].ToString(), this.ViewState["SortOrder"].ToString());
        //}  
        ////DTPOMain.DefaultView.Sort = "PoDate desc";
        //grdInternational.DataSource = DTPOMain;
        //grdInternational.DataBind(); 
        #endregion
    }
    //Domestic
    protected void grdDisplay_OnPageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grdDisplay.PageIndex = e.NewPageIndex;
        BindCustomData();
    }
    protected void grdDisplay_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.ToString() == "editpo")
        {
            string key = e.CommandArgument.ToString();            
            Response.Redirect("PurcahseManager.aspx?Poid=" + key.ToString());
        }
        if (e.CommandName.Equals("Sort"))
        {
            if (ViewState["SortExpression"] != null)
            {
                if (this.ViewState["SortExpression"].ToString() == e.CommandArgument.ToString())
                {
                    if (ViewState["SortOrder"].ToString() == "ASC")
                        ViewState["SortOrder"] = "DESC";
                    else
                        ViewState["SortOrder"] = "ASC";
                }
                else
                {
                    ViewState["SortOrder"] = "ASC";
                    ViewState["SortExpression"] = e.CommandArgument.ToString();
                }
            }
            else
            {
                ViewState["SortExpression"] = e.CommandArgument.ToString();
                ViewState["SortOrder"] = "ASC";
            }
        }
        BindCustomData();
    }    
    protected void grdDisplay_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //DataTable DT = (DataTable)grdDisplay.DataSource;
            string PoId = grdDisplay.DataKeys[e.Row.RowIndex].Value.ToString();
            strsql = " Select Status,stages from ACC_POMAIN where POID='" + PoId + "'";
            DataTable DT = DU.getdata(strsql);
            if (DT.Rows.Count > 0)
            {                
                Label lblStatusOfPO = (Label)e.Row.Cells[4].FindControl("lblStatusOfPO");
                if (DT.Rows[0]["Status"].ToString() == "1")
                {
                    e.Row.Cells[5].FindControl("imgLock").Visible = false;
                    e.Row.Cells[5].FindControl("lnkEdit").Visible = true;
                    lblStatusOfPO.Text = "Pending for Manager Approval";
                }
                if (DT.Rows[0]["Status"].ToString() == "2")
                {
                    e.Row.Cells[5].FindControl("imgLock").Visible = false;
                    e.Row.Cells[5].FindControl("lnkEdit").Visible = true;
                    lblStatusOfPO.Text = "Pending for Manager Approval";
                }
                if (DT.Rows[0]["Status"].ToString() == "3")//For Manager Approval
                {
                    e.Row.Cells[5].FindControl("imgLock").Visible = true;
                    e.Row.Cells[5].FindControl("lnkEdit").Visible = false;
                    lblStatusOfPO.Text = "Sent To Finance.";
                }
                if (DT.Rows[0]["Status"].ToString() == "4")//for Manager Revision
                {
                    e.Row.Cells[5].FindControl("imgLock").Visible = true;
                    e.Row.Cells[5].FindControl("lnkEdit").Visible = false;                    
                    lblStatusOfPO.Text = "Sent To Operator From Manager";
                }
                if (DT.Rows[0]["Status"].ToString() == "5")
                {
                    e.Row.Cells[5].FindControl("imgLock").Visible = false;
                    e.Row.Cells[5].FindControl("lnkEdit").Visible = false;  
                    lblStatusOfPO.Text = "Sent To Admin";
                }
                if (DT.Rows[0]["Status"].ToString() == "6")
                {
                    e.Row.Cells[5].FindControl("imgLock").Visible = false;
                    e.Row.Cells[5].FindControl("lnkEdit").Visible = true;  
                    lblStatusOfPO.Text = "Sent To Manager From Finance";
                }
                if (DT.Rows[0]["Status"].ToString() == "7")
                {
                    e.Row.Cells[5].FindControl("imgLock").Visible = true;
                    e.Row.Cells[5].FindControl("lnkEdit").Visible = false;  
                    lblStatusOfPO.Text = "Reject By Finance.";
                }
                if (DT.Rows[0]["Status"].ToString() == "8")
                {
                    e.Row.Cells[5].FindControl("imgLock").Visible = true;
                    e.Row.Cells[5].FindControl("lnkEdit").Visible = false;  
                    lblStatusOfPO.Text = "Approved By Admin";
                }
                if (DT.Rows[0]["Status"].ToString() == "9")
                {
                    e.Row.Cells[5].FindControl("imgLock").Visible = false;
                    e.Row.Cells[5].FindControl("lnkEdit").Visible = true;  
                    lblStatusOfPO.Text = "Sent For Revision By Admin.";
                }
                if (DT.Rows[0]["Status"].ToString() == "10")
                {
                    e.Row.Cells[5].FindControl("imgLock").Visible = true;
                    e.Row.Cells[5].FindControl("lnkEdit").Visible = false;  
                    lblStatusOfPO.Text = "Reject By Admin.";
                }
            }
            GridView GrdPOChild = (GridView)e.Row.FindControl("gvPORemarksChild");
            string SqlChildRemarks = "select POSTAGE,REMARKS,REVISIONREMARK,REVISIONORDER,REJECTREMARK,CDATE from ACC_POREMARKS where POID='" + PoId + "'";
            DataTable DTPurchaseRemarks = DU.getdata(SqlChildRemarks);
            DataTable DTChildPO = new DataTable();
            DTChildPO.Columns.Add("POSTAGE", typeof(string));
            DTChildPO.Columns.Add("REMARKS", typeof(string));           
            DTChildPO.Columns.Add("REVISIONORDER", typeof(string));            
            DTChildPO.Columns.Add("CDATE", typeof(string));
            DTChildPO.Columns.Add("CTIME", typeof(string));
            if (DTPurchaseRemarks.Rows.Count > 0)
            {
                for (int i = 0; i < DTPurchaseRemarks.Rows.Count; i++)
                {
                    DataRow DR = DTChildPO.NewRow();
                    if (DTPurchaseRemarks.Rows[i]["POSTAGE"].ToString() == "2")
                    {
                        DR["POSTAGE"] = "Manager";
                    }
                    if (DTPurchaseRemarks.Rows[i]["POSTAGE"].ToString() == "3")
                    {
                        DR["POSTAGE"] = "Finance";
                    }
                    if (DTPurchaseRemarks.Rows[i]["POSTAGE"].ToString() == "4")
                    {
                        DR["POSTAGE"] = "Admin";
                    }
                    //DR["POSTAGE"] = DTPurchaseRemarks.Rows[i]["POSTAGE"].ToString();
                    DR["REMARKS"] = DTPurchaseRemarks.Rows[i]["REMARKS"].ToString();                   
                    DR["REVISIONORDER"] = DTPurchaseRemarks.Rows[i]["REVISIONORDER"].ToString();                    
                    DR["CDATE"] = Convert.ToDateTime(DTPurchaseRemarks.Rows[i]["CDATE"].ToString()).ToString("dd-MMM-yyyy");
                    DR["CTIME"] = Convert.ToDateTime(DTPurchaseRemarks.Rows[i]["CDATE"].ToString()).ToString("hh:mm:ss tt");
                    DTChildPO.Rows.Add(DR);
                }
            }
            GrdPOChild.DataSource = DTChildPO;
            GrdPOChild.DataBind();
        }
    }
    protected void custPager_PageChanged(object sender, CustomPageChangeArgs e)
    {
        grdDisplay.PageSize = e.CurrentPageSize;
        grdDisplay.PageIndex = e.CurrentPageNumber;
        BindCustomData();
    }
    protected void grdDisplay_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType != DataControlRowType.Pager)
        {
            if (e.Row.RowState == DataControlRowState.Alternate)
            {
                e.Row.Attributes.Add("onmouseover", "this.className='rowhover'");
                e.Row.Attributes.Add("onmouseout", "this.className='gridview_alternate'");
                //e.Row.Attributes.Add("onclick", "this.className='rowselected'");
            }
            else if (e.Row.RowState == DataControlRowState.Normal)
            {
                e.Row.Attributes.Add("onmouseover", "this.className='rowhover'");
                e.Row.Attributes.Add("onmouseout", "this.className='gridviewitem'");
                //e.Row.Attributes.Add("onclick", "this.className='rowselected'");
            }
        }
    }
    //International
    protected void grdInternational_OnPageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grdInternational.PageIndex = e.NewPageIndex;
        BindInternationalPO();
    }
    protected void grdInternational_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.ToString() == "editpo")
        {
            string key = e.CommandArgument.ToString();
            Response.Redirect("PurcahseManager.aspx?Poid=" + key.ToString());
        }
        if (e.CommandName.Equals("Sort"))
        {
            if (ViewState["SortExpression"] != null)
            {
                if (this.ViewState["SortExpression"].ToString() == e.CommandArgument.ToString())
                {
                    if (ViewState["SortOrder"].ToString() == "ASC")
                        ViewState["SortOrder"] = "DESC";
                    else
                        ViewState["SortOrder"] = "ASC";
                }
                else
                {
                    ViewState["SortOrder"] = "ASC";
                    ViewState["SortExpression"] = e.CommandArgument.ToString();
                }
            }
            else
            {
                ViewState["SortExpression"] = e.CommandArgument.ToString();
                ViewState["SortOrder"] = "ASC";
            }
        }
        BindInternationalPO();
    }
    protected void grdInternational_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //DataTable DT = (DataTable)grdDisplay.DataSource;
            string PoId = grdInternational.DataKeys[e.Row.RowIndex].Value.ToString();
            strsql = " Select Status,stages from ACC_POMAIN where POID='" + PoId + "'";
            DataTable DT = DU.getdata(strsql);
            if (DT.Rows.Count > 0)
            {
                Label lblStatusOfPO = (Label)e.Row.Cells[4].FindControl("lblStatusOfPO");
                if (DT.Rows[0]["Status"].ToString() == "1")
                {
                    e.Row.Cells[5].FindControl("imgLock").Visible = false;
                    e.Row.Cells[5].FindControl("lnkEdit").Visible = true;
                    lblStatusOfPO.Text = "Pending for Manager Approval";
                }
                if (DT.Rows[0]["Status"].ToString() == "2")
                {
                    e.Row.Cells[5].FindControl("imgLock").Visible = false;
                    e.Row.Cells[5].FindControl("lnkEdit").Visible = true;
                    lblStatusOfPO.Text = "Pending for Manager Approval";
                }
                if (DT.Rows[0]["Status"].ToString() == "3")//For Manager Approval
                {
                    e.Row.Cells[5].FindControl("imgLock").Visible = true;
                    e.Row.Cells[5].FindControl("lnkEdit").Visible = false;
                    lblStatusOfPO.Text = "Sent To Finance.";
                }
                if (DT.Rows[0]["Status"].ToString() == "4")//for Manager Revision
                {
                    e.Row.Cells[5].FindControl("imgLock").Visible = true;
                    e.Row.Cells[5].FindControl("lnkEdit").Visible = false;
                    lblStatusOfPO.Text = "Sent To Operator From Manager";
                }
                if (DT.Rows[0]["Status"].ToString() == "5")
                {
                    e.Row.Cells[5].FindControl("imgLock").Visible = false;
                    e.Row.Cells[5].FindControl("lnkEdit").Visible = false;
                    lblStatusOfPO.Text = "Sent To Admin";
                }
                if (DT.Rows[0]["Status"].ToString() == "6")
                {
                    e.Row.Cells[5].FindControl("imgLock").Visible = false;
                    e.Row.Cells[5].FindControl("lnkEdit").Visible = true;
                    lblStatusOfPO.Text = "Sent To Manager From Finance";
                }
                if (DT.Rows[0]["Status"].ToString() == "7")
                {
                    e.Row.Cells[5].FindControl("imgLock").Visible = true;
                    e.Row.Cells[5].FindControl("lnkEdit").Visible = false;
                    lblStatusOfPO.Text = "Reject By Finance.";
                }
                if (DT.Rows[0]["Status"].ToString() == "8")
                {
                    e.Row.Cells[5].FindControl("imgLock").Visible = true;
                    e.Row.Cells[5].FindControl("lnkEdit").Visible = false;
                    lblStatusOfPO.Text = "Approved By Admin";
                }
                if (DT.Rows[0]["Status"].ToString() == "9")
                {
                    e.Row.Cells[5].FindControl("imgLock").Visible = false;
                    e.Row.Cells[5].FindControl("lnkEdit").Visible = true;
                    lblStatusOfPO.Text = "Sent For Revision By Admin.";
                }
                if (DT.Rows[0]["Status"].ToString() == "10")
                {
                    e.Row.Cells[5].FindControl("imgLock").Visible = true;
                    e.Row.Cells[5].FindControl("lnkEdit").Visible = false;
                    lblStatusOfPO.Text = "Reject By Admin.";
                }
            }
            GridView GrdPOChild = (GridView)e.Row.FindControl("gvPORemarksChild");
            string SqlChildRemarks = "select POSTAGE,REMARKS,REVISIONREMARK,REVISIONORDER,REJECTREMARK,CDATE from ACC_POREMARKS where POID='" + PoId + "'";
            DataTable DTPurchaseRemarks = DU.getdata(SqlChildRemarks);
            DataTable DTChildPO = new DataTable();
            DTChildPO.Columns.Add("POSTAGE", typeof(string));
            DTChildPO.Columns.Add("REMARKS", typeof(string));
            DTChildPO.Columns.Add("REVISIONORDER", typeof(string));
            DTChildPO.Columns.Add("CDATE", typeof(string));
            DTChildPO.Columns.Add("CTIME", typeof(string));
            if (DTPurchaseRemarks.Rows.Count > 0)
            {
                for (int i = 0; i < DTPurchaseRemarks.Rows.Count; i++)
                {
                    DataRow DR = DTChildPO.NewRow();
                    if (DTPurchaseRemarks.Rows[i]["POSTAGE"].ToString() == "2")
                    {
                        DR["POSTAGE"] = "Manager";
                    }
                    if (DTPurchaseRemarks.Rows[i]["POSTAGE"].ToString() == "3")
                    {
                        DR["POSTAGE"] = "Finance";
                    }
                    if (DTPurchaseRemarks.Rows[i]["POSTAGE"].ToString() == "4")
                    {
                        DR["POSTAGE"] = "Admin";
                    }
                    //DR["POSTAGE"] = DTPurchaseRemarks.Rows[i]["POSTAGE"].ToString();
                    DR["REMARKS"] = DTPurchaseRemarks.Rows[i]["REMARKS"].ToString();
                    DR["REVISIONORDER"] = DTPurchaseRemarks.Rows[i]["REVISIONORDER"].ToString();
                    DR["CDATE"] = Convert.ToDateTime(DTPurchaseRemarks.Rows[i]["CDATE"].ToString()).ToString("dd-MMM-yyyy");
                    DR["CTIME"] = Convert.ToDateTime(DTPurchaseRemarks.Rows[i]["CDATE"].ToString()).ToString("hh:mm:ss tt");
                    DTChildPO.Rows.Add(DR);
                }
            }
            GrdPOChild.DataSource = DTChildPO;
            GrdPOChild.DataBind();
        }
    }
    protected void grdInternational_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType != DataControlRowType.Pager)
        {
            if (e.Row.RowState == DataControlRowState.Alternate)
            {
                e.Row.Attributes.Add("onmouseover", "this.className='rowhover'");
                e.Row.Attributes.Add("onmouseout", "this.className='gridview_alternate'");
                //e.Row.Attributes.Add("onclick", "this.className='rowselected'");
            }
            else if (e.Row.RowState == DataControlRowState.Normal)
            {
                e.Row.Attributes.Add("onmouseover", "this.className='rowhover'");
                e.Row.Attributes.Add("onmouseout", "this.className='gridviewitem'");
                //e.Row.Attributes.Add("onclick", "this.className='rowselected'");
            }
        }
    }
}
